﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MyVoteMVC.BackstageManagement.Controllers;
using MyVoteMVC.Domain;

namespace MyVoteMVC.BackstageManagement.Views
{
    public partial class UpdateActivity : Form, IActivityView
    {
        public event EventHandler OnUpdateComplete;

        private IActivityController controller;

        private string activityId;
        
        public UpdateActivity(string activityId)
        {
            InitializeComponent();
            controller = new ActivityController(this);
            this.activityId = activityId;
        }

        public void AddActivityComplete(string message)
        {
            throw new NotImplementedException();
        }

        public void ModifyActivityComplete(string message)
        {
            MessageBox.Show(message);
        }

        public void GetActivityComplete(VT_Activities activity)
        {
            txtContent.Text = activity.Activity_Content;
            txtTitle.Text = activity.Activity_Title;
            cbMultiSubmit.Checked = (activity.Activity_IfMultiSubmit == 1 ? true : false);
            cbReadAll.Checked = (activity.Activity_IfReadAll == 1 ? true : false);
            cbShowUser.Checked = (activity.Activity_IfShowUser == 1 ? true : false);
            dtEnd.Value = activity.Activity_EndTime.Value;
            dtStart.Value = activity.Activity_StartTime.Value;
        }

        void Clear()
        {
            txtContent.Text = "";
            txtTitle.Text = "";
            cbMultiSubmit.Checked = false;
            cbReadAll.Checked = false;
            cbShowUser.Checked = false;
            dtEnd.Value = DateTime.Now;
            dtStart.Value = DateTime.Now;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
           
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(activityId))
            {
                controller.ModifyActivity(activityId,
                    txtTitle.Text,
                    txtContent.Text,
                    dtStart.Value.Date,
                    dtEnd.Value.Date,
                      txtImage.Text,
                    cbShowUser.Checked ? 1 : 0,
                    cbReadAll.Checked ? 1 : 0,
                    cbMultiSubmit.Checked ? 1 : 0);
            }
        }

        private void UpdateActivity_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(activityId))
            {
                controller.GetActivity(activityId);
            }
        }

        private void btnImage_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "JPEG文件(*.jpg)|*.jpg|PNG文件(*.png)|*.png|GIF文件(*.gif)|*.gif";
            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string filePath = openFileDialog1.FileName;
                txtImage.Text = filePath;
            }
        }

        private void UpdateActivity_FormClosing(object sender, FormClosingEventArgs e)
        {
            OnUpdateComplete(null, null);
        }

        public void OnSubmitError(string message)
        {
            MessageBox.Show(message);
        }
    }
}
